package com.example.ssl;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

public class Https {


	
	//JAVA操作SSL协议，通过Socket访问Https
	
	public String socketHttp(String url,int port,String param){
    try{
		// 创建SSLContext对象，并使用我们指定的信任管理器初始化 
        TrustManager[] tm = { new MyX509TrustManager() }; 
        //获得上下文
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); 
        sslContext.init(null, tm, new java.security.SecureRandom()); 
        // 从上述SSLContext对象中得到SSLSocketFactory对象 
        SSLSocketFactory ssf = sslContext.getSocketFactory(); 
     // 从工厂获得Socket连接         
     Socket socket = ssf.createSocket(url, port);
     /*PrintWriter out=new PrintWriter(socket.getOutputStream());
     out.print(param);*/
     OutputStreamWriter reqOut = null;
		if (param != null) {
			reqOut = new OutputStreamWriter(socket.getOutputStream());
			reqOut.write(param);
			reqOut.flush();
		}
		int charCount = -1;
		InputStream in = socket.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
		StringBuffer responseMessage = new StringBuffer();
		while ((charCount = br.read()) != -1) {
			responseMessage.append((char) charCount);
		}
		in.close();
		if (reqOut != null)
			reqOut.close();
		return responseMessage.toString();
     
     
    }catch(Exception e){
    	e.printStackTrace();
    	return null;
    }
	}
	
	public String setHttps(String url,String param,String method){
		try{
			URL myURL = new URL(url); 
	        // 创建HttpsURLConnection对象，并设置其SSLSocketFactory对象 
	        HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
	        // 取得该连接的输入流，以读取响应内容 
	        httpsConn.setConnectTimeout(5 * 1000);
	        httpsConn.setRequestMethod(method);
	        httpsConn.setDoOutput(true);//允许输出数据
	        httpsConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	        httpsConn.setRequestProperty("Content-Length", String.valueOf(param.length()));
	        OutputStream outStream = httpsConn.getOutputStream();
	        outStream.write(param.getBytes());
	        outStream.flush();
	        outStream.close();

	        InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream()); 
	        // 读取服务器的响应内容并显示 
	        int respInt = insr.read(); 
	        StringBuffer sb=new StringBuffer();
	        while (respInt != -1) { 
	            sb.append((char) respInt);
	            respInt = insr.read(); 
	        } 
	        return sb.toString();
		}catch(Exception e){
			e.printStackTrace();
			}
		return null;
		}
	
				

}
